<?php
	if (!$_POST["keyword"]) return;
	
	include "../config.php";
	
	$keyword = "%".$_POST['keyword']."%";
	$limit = $_POST['maxrow'];
	
	$sql_destinations = mysql_query("
		SELECT
		destinations.id,
		destinations.destination_name,
		destinations.slug
		FROM
		destinations
		WHERE
		destinations.destination_name LIKE '$keyword'
		LIMIT 0, $limit
	");
	
	$count_destination = mysql_num_rows($sql_destinations);
	
	if($count_destination < $limit){
		$limit = $limit - $count_destination;
	}
	
	$sql_hotels = mysql_query("
		SELECT
		destinations.id AS destination_id,
		destinations.destination_name,
		hotels.id,
		hotels.hotel_name,
		hotels.hotel_slug
		FROM
		hotels
		Inner Join destinations ON hotels.destination_id = destinations.id
		WHERE
		hotels.hotel_name LIKE '$keyword'
		LIMIT 0, $limit

	");
	
	// Build Array
	$result = array();
	
	while($des = mysql_fetch_array($sql_destinations)){
		$result[] = array(
			"name" => $des['destination_name'],
			"location" => "",
			"destination_slug" => $des["slug"],
			"hotel_slug" => ""
		);
	}
	
	while($ht = mysql_fetch_array($sql_hotels)){
		$result[] = array(
			"name" => $ht['hotel_name'],
			"location" => $ht['destination_name'],
			"destination_slug" => "",
			"hotel_slug" => $ht["hotel_slug"]
		);
	}
	
	$result = array(
		"hotels" => $result
	);
	
	echo json_encode($result);
?>